package com.xiyun.easyapi.service;


import com.mybatisflex.core.row.Db;
import com.xiyun.easyapi.config.util.Current;
import com.xiyun.easyapi.config.util.Res;
import com.xiyun.easyapi.config.web.CustomException;
import com.xiyun.easyapi.controller.request.SQLReq;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import javax.sql.DataSource;

@Service
public class SQLService {
    @Autowired
    private DataSource dataSource;

    @SneakyThrows
    public Res<?> execute(SQLReq.execute param) {
        executeSql(param.getSql());
        return Res.success();
    }

    @SneakyThrows
    public void executeSql(String sql) {
        if (!Current.getUser()
                .getAdmin()) throw new CustomException("仅管理员可操作");
        Db.updateBySql(sql);
    }
}
